package com.symaster.common.swing.element.button;

import com.symaster.common.swing.config.AppConfig;
import com.symaster.common.swing.style.Style;
import com.symaster.common.swing.util.SwingUtil;
import com.symaster.common.swing.util.ThreadPoolUtil;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.util.EventListener;
import java.util.HashSet;
import java.util.Set;

public class SyzdButton extends JComponent {

    private JButton button;
    private Set<ClickListener> clickListeners;
    private String text;
    private Icon icon;
    private String iconName;
    private Dimension iconSize = new Dimension(25, 25);

    public SyzdButton() {
        initPage();
        initListener();
    }

    public SyzdButton(String text) {
        this.text = text;
        initPage();
        initListener();
    }

    public SyzdButton(Icon icon) {
        this.icon = icon;
        initPage();
        initListener();
    }

    @Override
    public Dimension getPreferredSize() {
        return super.getPreferredSize();
    }

    @Override
    public void setPreferredSize(Dimension preferredSize) {
        super.setPreferredSize(preferredSize);
        button.setPreferredSize(preferredSize);
    }

    @Override
    public Color getBackground() {
        return super.getBackground();
    }

    @Override
    public void setBackground(Color bg) {
        super.setBackground(bg);
        button.setBackground(bg);
    }

    public Dimension getIconSize() {
        return iconSize;
    }

    public void setIconSize(Dimension iconSize) {
        this.iconSize = iconSize;
        if (iconName != null) {
            setIconName(iconName);
        }
    }

    public String getIconName() {
        return iconName;
    }

    public void setIconName(String iconName) {
        this.iconName = iconName;
        setIcon(SwingUtil.loadIcon(iconName, iconSize.width, iconSize.height));
    }

    public Icon getIcon() {
        return icon;
    }

    public void setIcon(Icon icon) {
        this.icon = icon;
        button.setIcon(icon);
    }

    private void initListener() {
        button.addActionListener((e) -> clickListener());
    }

    private void initPage() {
        setLayout(new BorderLayout());
        setBackground(AppConfig.getColor(AppConfig.AUXILIARY_BACKGROUND_COLOR));

        if (StringUtils.isNotEmpty(text)) {
            button = new JButton(text);
        } else if (icon != null) {
            button = new JButton(icon);
        } else {
            button = new JButton();
        }
        Style.setBorder(1, button);
        Style.setFont(1, button);
        button.setFocusPainted(false);
        button.setBackground(AppConfig.getColor(AppConfig.AUXILIARY_BACKGROUND_COLOR));
        button.setForeground(AppConfig.getColor(AppConfig.AUXILIARY_FONT_COLOR));
        add(button, BorderLayout.CENTER);
    }

    private void clickListener() {
        if (clickListeners == null) {
            return;
        }
        clickListeners.forEach((e) -> ThreadPoolUtil.execute(e::handler));
    }

    public void setForeground(Color color) {
        button.setForeground(color);
    }

    public boolean isEnabled() {
        return button.isEnabled();
    }

    public void setEnabled(boolean enabled) {
        button.setEnabled(enabled);
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
        button.setText(text);
    }

    public void addClickListener(ClickListener listener) {
        if (clickListeners == null) {
            clickListeners = new HashSet<>();
        }
        clickListeners.add(listener);
    }

    public void removeClickListener(ClickListener listener) {
        if (clickListeners == null) {
            return;
        }
        clickListeners.remove(listener);
    }

    public interface ClickListener extends EventListener {
        void handler();
    }
}
